import request from "@/utils/request";
import configUrl from "@/common/config";
let SSEChat: any;
interface SSEOptionFace {
  /**
   * 消息
   */
  msg: string;
  /**
   * 数据流的回调
   */
  callback?: Function;
  /**
   * 连接地址
   */
  url?: string;
}
// #ifdef H5

SSEChat = (option: SSEOptionFace) => {
  console.log("我到这55555里来了");
  let { msg, callback } = option;
  return new Promise(async (resolve, reject) => {
    const response = await fetch(`${configUrl.hosts}/chat/send`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ msg }),
    });
    if (!response.body) return;
    const reader = response.body.getReader();

    while (true) {
      const { done, value } = await reader.read();
      console.log(value);
      const decoder = new TextDecoder("utf-8");
      const text = decoder.decode(value);
      callback && callback(text);

      if (done) break;
    }
    //
    resolve("");
  });
};
// #endif
// #ifndef H5

SSEChat = (option: SSEOptionFace) => {
  let { msg, callback } = option;
  console.log("我到这里ffffffffffffffffffffff来了");

  return new Promise(async (resolve, reject) => {
    const requestTask = wx.request({
      url: configUrl.hosts + "/chat/send", //路径
      method: "POST",
      header: {
        Authorization: "",
      },
      enableChunked: true,
      data: {
        msg,
      },
      success: (response) => {
        console.log(
          "【接口结束】免费的www.yuxin-ai.com/fastapi/api/chat接口成功的response，这是接口接收完毕success",
          response,
          "requestTask",
          requestTask
        );
        resolve({});
      },
      fail: (error) => {
        console.log("接口错误了error", error);
      },
      complete: () => {},
    });
    requestTask.onHeadersReceived(function (res) {
      console.log("流式header请求前res", res);
    });
    requestTask.onChunkReceived(function (res) {
      console.log(res);

      const decoder = new TextDecoder();
      const text = decoder.decode(res.data);
      callback && callback(text);
    });
  });
};

// #endif

const SSEChatVoice =(option: SSEOptionFace) => {
  let { msg, callback } = option;
  console.log("我到这里ffffffffffffffffffffff来了");

  return new Promise(async (resolve, reject) => {
    const requestTask = wx.request({
      url: configUrl.hosts + "/chat/voice", //路径
      method: "POST",
      header: {
        Authorization: "",
      },
      enableChunked: true,
      data: {
        msg,
      },
      success: (response) => {
    
        resolve({});
      },
      fail: (error) => {
        console.log("接口错误了error", error);
      },
      complete: () => {},
    });
    requestTask.onHeadersReceived(function (res) {

    });
    requestTask.onChunkReceived(function (res) {
      console.log('什么东西');
      
      console.log(res);

      const decoder = new TextDecoder();
      const text = decoder.decode(res.data);
      console.log(text);
      
      callback && callback(text);
    });
  });
};


export { SSEChat, SSEChatVoice };
